package com.zhongzhi.serviceimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.dao.StationDao;
import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.dao.EquipmentDao;
import com.zhongzhi.service.EquipmentService;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.EquipmentShow;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.util.Bo;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import lombok.extern.log4j.Log4j;

@Log4j
@Service
public class EquipmentServiceImpl implements EquipmentService{

	@Autowired
	private EquipmentDao equipmentDao;
	@Autowired
	private StationDao stationDao;
	//增
	@Override
	public Bo insert(Equipment equipment) {
		log.info("后端:设备:新增:"+equipment);
		//测试
		//		System.out.println(equipment);
		//		return new Bo(true,"输入ip地址格式不正确",0,equipment);
		//参数校验
		if (equipment.getPlcName()==null || equipment.getPlcName().equals("")) {
			return new Bo(false, 400, "设备名称不能为空", 0,null);
		}else if (equipment.getHost().matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")==false) {
			return new Bo(false, 400, "IP地址配置格式不正确", 0,null);
		}else if (equipment.getPort()==null) {
			return new Bo(false, 400, "端口不能为空", 0,null);
		}else if (equipment.getPort()<0 || equipment.getPort()>65535) {
			return new Bo(false, 400, "端口范围不正确", 0,null);
		}else if (equipment.getUpper()==null || equipment.getPlcName().equals("")) {
			return new Bo(false, 400, "上位机站号不能为空", 0,null);
		}else {
			//执行存入数据库
			JsonResult insertServer = insertServer(equipment);
			if (insertServer.getCode()!=200) {
				log.error("后端:设备:新增失败:"+0);
				return new Bo(false, 500, "存储数据失败", 0,null);
			}else {
				log.info("后端:设备:新增成功");
				return new Bo(false, 200, "存储数据成功", 1,0);
			}
		}
	}
	//改
	@Override
	public Bo update(Equipment equipment) {
		//参数校验
		log.info("后端:设备:修改:"+equipment);
		if (equipment.getPlcName()==null || equipment.getPlcName().equals("")) {
			return new Bo(false, 400, "设备名称不能为空", 0,null);
		}else if (equipment.getHost().matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")==false) {
			return new Bo(false, 400, "IP地址配置格式不正确", 0,null);
		}else if (equipment.getPort()==null) {
			return new Bo(false, 400, "端口不能为空", 0,null);
		}else if (equipment.getPort()<0 || equipment.getPort()>65535) {
			return new Bo(false, 400, "端口范围不正确", 0,null);
		}else if (equipment.getUpper()==null || equipment.getPlcName().equals("")) {
			return new Bo(false, 400, "上位机站号不能为空", 0,null);
		}else {
			//执行存入数据库
			JsonResult insertServer = updateServer(equipment);
			if (insertServer.getCode()!=200) {
				log.error("后端:设备:修改失败:"+0);
				return new Bo(false, 500, "数据修改失败", 0,null);
			}else {
				log.info("后端:设备:修改成功:");
				return new Bo(false, 200, "数据修改成功", 1,0);
			}
		}
	}
	//删除
	@Override
	public Bo delete(Integer id) {
		log.info("后端:设备:根据id删除:"+id);
		if (id<=0) {
			return new Bo(false, 400, "删除id编号不能小于0", 0,null);
		}else {
			int selectEquipmentId=0;
			try {
				selectEquipmentId = stationDao.selectEquipmentId(id);
				log.info("后端:设备:根据id删除:查询此id下的数据数量:"+selectEquipmentId);
			} catch (Exception e) {
				log.error(""+e);
				return new Bo(false, 500, "由于查询该设备下设备信息失败,故删除失败", 1,e);
			}
			
			if (selectEquipmentId>0) {
				return new Bo(false, 400, "由于此设备下具有设备信息,不能删除该设备", 0,null);
			}else {
				JsonResult insertServer = deleteServer(id);
				if (insertServer.getCode()!=200) {
					log.error("后端:设备:删除失败:"+0);
					return new Bo(false, 500, "数据删除失败", 0,null);
				}else {
					log.info("后端:设备:删除成功:");
					return new Bo(false, 200, "数据删除成功", 1,0);
				}
			}
		}
	}
	//查询
	@Override
	public Bo select(Integer pages, Integer line) {
		log.info("后端:设备:分页查询:pages="+pages+",line="+line);
		if (pages==null) {
			pages=1;
		}
		if (line==null) {
			line=20;
		}
		int a = (pages-1)*line;
		
		try {
			List<EquipmentShow> select = equipmentDao.select(a,line);
			log.info("后端:设备:分页查询:pages="+pages+",line="+line+"成功:"+select);
			return new Bo(true, 200, "查询设备成功",select.size(),select);
		} catch (Exception e) {
			log.error("后端:设备:查询设备失败"+e);
			return new Bo(false, 500, "查询设备失败", 1,e);
		}
	}
	//查询设备总数
	@Override
	public Bo selectSize() {
		log.info("后端:设备:查询设备总数");
		try {
			int size = equipmentDao.selectSize();
			log.info("后端:设备:查询设备总数成功:"+size);
			return new Bo(true, 200, "查询设备数量成功",1,size);
		} catch (Exception e) {
			log.error("后端:设备:查询设备数量失败:"+e);
			return new Bo(false, 500, "查询设备数量失败", 1,e);
		}
	}
	//根据id查询数据
	@Override
	public Bo selectById(Integer id) {
		log.info("后端:设备:根据id查询数据:"+id);
		//数据校验
		if (id<=0) {
			return new Bo(false, 400, "查询设备id编号不能小于0", 0,null);
		}else {
			try {
				EquipmentShow eq = equipmentDao.selectById(id);
				log.info("后端:设备:根据id查询数据:"+id+"成功:"+eq);
				return new Bo(true, 200, "id查询设备数量成功",1,eq);
			} catch (Exception e) {
				log.error("后端:设备:id查询设备数量失败"+e);
				return new Bo(false, 500, "id查询设备数量失败", 1,e);
			}
		}
	}
	
	//7:根据设备id集合查询
	@Override
	public Bo selectByIds(Integer[] ids) {
		log.info("后端:设备:根据计算机id集合查询设备:"+ids);
		if (ids!=null && ids.length!=0) {
			try {
				List<EquipmentShow> select = equipmentDao.selectByIds(ids);
				log.info("后端:设备:根据计算机id集合查询设备:"+ids+"成功:"+select);
				return new Bo(true, 200, "根据计算机查询设备成功", select.size(), select);
			} catch (Exception e) {
				log.error("后端:设备:根据计算机id集合查询设备失败"+e);
				return new Bo(false, 500, "根据计算机查询设备失败", 1, e);
			}
			
		}else {
			return new Bo(false, 400, "参数为空", 1, null);
		}
	}
	@Override
	public Bo webinsert(Equipment equipment) {
		log.info("后端:设备:服务器新增:"+equipment);
		try {
			int a = equipmentDao.insert(equipment);
			log.info("后端:设备:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:设备:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webupdate(Equipment equipment) {
		log.info("后端:设备:服务器修改:"+equipment);
		try {
			int a = equipmentDao.update(equipment);
			log.info("后端:设备:服务器修改成功:"+a);
			return new Bo(true, 200, "服务器修改成功",1,a);
		} catch (Exception e) {
			log.error("后端:设备:服务器修改失败:"+e);
			return new Bo(false, 500, "服务器修改失败", 1,e);
		}
	}
	@Override
	public Bo webdelete(Integer id) {
		log.info("后端:设备:服务器删除:"+id);
		try {
			int a = equipmentDao.delete(id);
			log.info("后端:设备:服务器删除成功:"+a);
			return new Bo(true, 200, "服务器删除成功",1,a);
		} catch (Exception e) {
			log.error("后端:设备:服务器删除失败:"+e);
			return new Bo(false, 500, "服务器删除失败", 1,e);
		}
	}
	@Override
	public Bo webselect() {
		log.info("后端:设备:服务器查询:");
		try {
			ArrayList<Equipment> list =equipmentDao.webselect();
			log.info("后端:设备:服务器查询成功:"+list);
			return new Bo(true, 200, "服务器查询成功",1,list);
		} catch (Exception e) {
			log.error("后端:设备:服务器查询失败:"+e);
			return new Bo(false, 500, "服务器查询失败", 1,e);
		}
	}
	
	
	private JsonResult insertServer(Equipment equipment) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/equipment/insert?id="+equipment.getId()
		+"&plcName="+equipment.getPlcName()
		+"&host="+equipment.getHost()
		+"&port="+equipment.getPort()
		+"&upper="+equipment.getUpper()
		+"&remarks="+equipment.getRemarks();
		try {
			log.info("后端:设备:同步新增:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:设备:同步新增成功:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = equipmentDao.insert(equipment);
				log.info("后端:设备:同步新增失败,:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:设备:同步新增:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
		
		
	}

	private JsonResult updateServer(Equipment equipment) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/equipment/update?id="+equipment.getId()
		+"&plcName="+equipment.getPlcName()
		+"&host="+equipment.getHost()
		+"&port="+equipment.getPort()
		+"&upper="+equipment.getUpper()
		+"&remarks="+equipment.getRemarks();
		try {
			log.info("后端:设备:同步修改:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:设备:同步修改:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = equipmentDao.update(equipment);
				log.info("后端:设备:同步修改:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:设备:同步修改:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
	}

	private JsonResult deleteServer(Integer id) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/equipment/delete?id="+id;
		try {
			log.info("后端:设备:同步删除:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:设备:同步删除:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = equipmentDao.delete(id);
				log.info("后端:设备:同步删除:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:设备:同步删除:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
	}


}


